<?php


namespace App\Utils\Wechat\OpenPlatform;


use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
use EasyWeChat\Kernel\Exceptions\RuntimeException;
use EasyWeChat\Kernel\Support\Collection;
use EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Application;
use GuzzleHttp\Exception\GuzzleException;
use Hyperf\Utils\Codec\Xml;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;

/**
 * Class MiniProgram
 * @package App\Utils\Wechat\OpenPlatform
 */
class MiniProgram
{
    /**
     * @var \EasyWeChat\OpenPlatform\Application|mixed
     */
    private $app;

    /**
     * @var OpenPlatform|mixed|ContainerInterface
     */
    private $openPlatform;


    public function __construct()
    {
        $this->openPlatform = di(OpenPlatform::class);
        $this->app = $this->openPlatform->getApp();
    }

    /**
     * 实例化一个小程序客户端
     * @param $appID
     * @return Application
     */
    public function account($appID): Application
    {
        $refreshToken = $this->openPlatform->authorizerRefreshToken($appID);
        return $this->app->miniProgram($appID, $refreshToken);
    }

    /**
     * @param string $appID
     * @param string $code
     * @return array|Collection|object|ResponseInterface|string
     * @throws InvalidConfigException
     */
    public function login(string $appID, string $code)
    {
        return $this->account($appID)->auth->session($code);
    }

    /**
     * 上传代码
     * @param string $appID
     * @param int $templateID
     * @param string $extJson
     * @return array|Collection|object|ResponseInterface|string
     * @throws GuzzleException
     * @throws InvalidConfigException
     */
    public function commit(string $appID, int $templateID, string $extJson)
    {
        $templateList = $this->openPlatform->templateList();
        $version = '';
        $description = '';
        foreach ($templateList as $template) {
            if ($template['template_id'] == $templateID) {
                $version = $template['user_version'];
                $description = $template['user_desc'];
            }
        }
        return $this->account($appID)->code->commit($templateID, $extJson, $version, $description);
    }


    /**
     * 发布审核
     * @param string $appID
     * @return array|Collection|object|ResponseInterface|string
     * @throws GuzzleException
     * @throws InvalidConfigException
     */
    public function submitAudit(string $appID)
    {
        return $this->account($appID)->code->submitAudit([]);
    }

    /**
     * 发布最新审核通过的版本
     * @param $appID
     * @return array|Collection|object|ResponseInterface|string
     * @throws GuzzleException
     * @throws InvalidConfigException
     */
    public function release($appID)
    {
        return $this->account($appID)->code->release();
    }

    /**
     * @param string $appID
     * @return \EasyWeChat\Kernel\Http\Response
     * @throws GuzzleException
     * @throws InvalidConfigException
     */
    public function getQrCode(string $appID)
    {
        return $this->account($appID)->code->getQrCode();
    }


    /**
     * 消息解密
     * @param $appID
     * @param $encrypt
     * @param $msgSignature
     * @param $nonce
     * @param $timestamp
     * @return mixed
     * @throws RuntimeException
     */
    public function decrypt($appID, $encrypt, $msgSignature, $nonce, $timestamp)
    {
        $xml = $this->account($appID)->encryptor->decrypt($encrypt,$msgSignature, $nonce, $timestamp);
        return Xml::toArray($xml);

    }

    public function send()
    {
        $this->account('fdsf')->customer_service->send([
            [
                'touser' => 'wxd101a85aa106f53e'
            ]
        ]);
    }
}